package com.lebang.medicalInquiry.service;

import com.lebang.medicalInquiry.component.RedisComponent;
import com.lebang.medicalInquiry.constant.RedisKeyEnum;
import com.lebang.medicalInquiry.mapper.ArticleClassifyMapper;
import com.lebang.medicalInquiry.mapper.ArticleMapper;
import com.lebang.medicalInquiry.model.po.Article;
import com.lebang.medicalInquiry.model.po.ArticleClassify;
import com.lebang.medicalInquiry.model.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @version V1.0
 * @author: zpc
 * @date:
 */
@Service
public class ArticleService {
    @Resource
    private ArticleMapper mapper;

    @Resource
    private ArticleClassifyMapper articleClassifyMapper;

    @Autowired
    private RedisComponent redisComponent;


    public PageVo<List<Article>> getList(String keyWord, Integer state, Integer classifyId, Integer page, Integer limit) {
        List<Article> list = mapper.listByCondition(keyWord, classifyId, state, (page - 1) * limit, limit);
        Integer count = mapper.countByCondition(keyWord, classifyId, state);
        return new PageVo<>(list, count);
    }

    // 添加风格 需要把现有题材的都进行同步
    public void operator(Article po) {
        if (po.getId() == null) {
            po.setCreateTime(LocalDateTime.now());
            po.setUpdateTime(LocalDateTime.now());
            po.setState(1);
            mapper.insertSelective(po);

        } else {
            po.setUpdateTime(LocalDateTime.now());
            mapper.updateByPrimaryKey(po);
        }


    }

    // 删除
    public void remove(Integer id) {
        mapper.deleteByPrimaryKey(id);

    }

    public PageVo<List<ArticleClassify>> listArticleClassify(String keyWord, Integer state, Integer page, Integer limit) {
        List<ArticleClassify> list = articleClassifyMapper.listByCondition(keyWord, state, (page - 1) * limit, limit);
        Integer count = articleClassifyMapper.countByCondition(keyWord, state);
        return new PageVo<>(list, count);

    }

    public void operatorClassify(ArticleClassify po) {

        if (po.getId() == null) {
            po.setCreateTime(LocalDateTime.now());
            po.setUpdateTime(LocalDateTime.now());
            po.setState(1);
            articleClassifyMapper.insertSelective(po);

        } else {
            po.setUpdateTime(LocalDateTime.now());
            articleClassifyMapper.updateByPrimaryKey(po);
        }
        redisComponent.del(RedisKeyEnum.article_classify_list.getName());
    }

    public void removeClassify(Integer id) {
        articleClassifyMapper.deleteByPrimaryKey(id);
        redisComponent.del(RedisKeyEnum.article_classify_list.getName());
    }
}
